<?php
	require_once(dirname(__DIR__) . "/include/forum.class.php");
	require_once(dirname(__DIR__) . "/include/thread.class.php");
	require_once(dirname(__DIR__) . "/include/post.class.php");
	require_once(dirname(__DIR__) . "/include/frontpage.class.php");
	require_once(dirname(__DIR__) . "/include/mysqlexecutor.class.php");
?>

<div id="navigaion">
	<a href="./index.php?sql=1">create</a><br />
	<a href="./index.php?sql=2">insert</a><br />
</div>

<?php
	if(isset($_GET['sql'])) {
		$mysqlexecutor = new MySQLExecutor();
		
		if($_GET['sql'] == 1) {
			$mysqlexecutor -> createForumDatabase();
		}
		else if($_GET['sql'] == 2) {
			$mysqlexecutor -> insertForumDatabase();
		}
	}
	if(isset($_GET['thread_id'])) {
		$thread_id = $_GET['thread_id'];
		
		if(isset($_GET['new'])) {
			insertPost($_GET['thread_id'], 1, $_POST['content']);
			header("Location: ./index.php?thread_id=". $thread_id);
		}
		else if(isset($_GET['delete'])) {
			deletePost($_GET['delete']);
			header("Location: ./index.php?thread_id=". $thread_id);
		}
		
		$thread = new Thread($_GET['thread_id']);
		$thread -> show();
	}
	else if(isset($_GET['forum_id'])) {
		$forum_id = $_GET['forum_id'];
		
		if(isset($_GET['new'])) {
			insertThread($forum_id, 1, $_POST['name']);
			
			$query = "SELECT MAX(thread_id) AS thread_id FROM t_thread";
			$result = mysql_query($query);
			$row = mysql_fetch_assoc($result);
			$thread_id = $row['thread_id'];
			
			insertPost($thread_id, 1, $_POST['content']);
			
			header("Location: ./index.php?forum_id=". $forum_id);
		}
		else if(isset($_GET['close'])) {
			closeThread($_GET['close']);
			header("Location: ./index.php?forum_id=". $forum_id);
		}
		
		$forum = new Forum($forum_id);
		$forum -> show();
	}
	else {
		$frontpage = new Frontpage();
		$frontpage -> show();
	}
?>